<template>
	<view class="class-container">
		<cc-navbar></cc-navbar>
		<u-sticky>
			<view class="class-container-header">
				<cc-search :border="false" placeholder="班级名称搜索" :value.sync="query.class_name" @search="getList(1)"
					@clear="getList(1)"></cc-search>
			</view>
		</u-sticky>
		<!-- @tap="handleDetail(row.id)" -->
		<view class="list" v-if="count">
			<view class="item" v-for="(row, index) in list" :key="index">
				<view class="item-top fcb" :style="{
						backgroundColor: row.class_color
					}">
					<view class="item-top-left">
						<text class="u-line-1">{{ row.class_name || '-' }}</text>
					</view>
				</view>
				<view class="item-content">
					<view class="progress">
						<view class="fcb mb10">
							<text>课程进度</text>
							<text
								:style="{ color: row.class_color }">{{ row.cla_course_status_1 }}/{{ row.cla_course_status_all }}课时</text>
						</view>
						<u-line-progress height="12rpx" :showText="false" :percentage="row.percentage"
							:activeColor="row.class_color"></u-line-progress>
					</view>
					<u-row gutter="12" align="top">
						<u-col span="6">
							<view class="cc-flex cc-items-center">
								<view class="font-info">老师:</view>
								<view class="u-line-1 cc-flex-1 cc-p-l-12">{{ row.teacher_name || '-' }}</view>
							</view>
						</u-col>
						<u-col span="6">
							<view class="cc-flex cc-items-center">
								<view class="font-info">课程:</view>
								<view class="u-line-1 cc-flex-1 cc-p-l-12">{{ row.course_name || '-' }}</view>
							</view>
						</u-col>
					</u-row>
					<u-row gutter="12" customStyle="margin-top: 24rpx;">
						<u-col span="6">
							<view class="cc-flex cc-items-center">
								<view class="font-info">人数/容量:</view>
								<view class="u-line-1 cc-flex-1 cc-p-l-12">
									{{ row.cla_student_count }}/{{ row.capacity }}
								</view>
							</view>
						</u-col>
						<u-col span="6">
							<view class="cc-flex cc-items-center">
								<view class="font-info">默认课时:</view>
								<view class="u-line-1 cc-flex-1 cc-p-l-12">{{ row.expend_hour }}</view>
							</view>
						</u-col>
					</u-row>
				</view>
				<view class="item-bottom">
					<u-row gutter="12" v-for="(ele, eindex) in action" :key="eindex"
						:customStyle="eindex !== 0 ? 'margin-top: 24rpx;' : ''">
						<u-col span="4" v-for="item in ele" :key="item.id">
							<u-button plain :icon="item.icon" size="small" type="info" :text="item.label"
								@click="handleRouter(item, row)"></u-button>
						</u-col>
						<u-col span="4" v-if="eindex === 1">
							<u-button plain icon="grid" size="small" type="primary" text="一键排课"
								@click="handleCreateClassrule(row.id)"></u-button>
						</u-col>
					</u-row>
				</view>
			</view>
		</view>
		<cc-loadmore show-limit="2" :loading="loading" :current="list.length" :count="count" />
		<cc-empty v-if="!loading && !count" mode="list" />
		<cc-create-classrule :showAction="false" ref="CreateClassruleRef"></cc-create-classrule>
	</view>
</template>

<script>
	import {
		index as getClass
	} from '@/codecat/api/education/class/index.js';
	export default {
		// 班级管理
		name: 'ClassIndex',
		data() {
			return {
				loading: false,
				list: [],
				count: 0,
				query: {
					class_name: '',
					page: 1,
					limit: 10,
					status: null
				},
				action: [
					[{
							id: '1',
							label: '课程表',
							path: '/class-time',
							icon: 'calendar'
						},
						{
							id: '2',
							label: '学员',
							path: '/class-student',
							icon: 'account'
						},
						{
							id: '3',
							label: '上课记录',
							path: '/attend-list',
							icon: 'clock'
						}
					],
					[{
						id: '3',
						label: '排课计划',
						path: '/class-rule',
						icon: 'list-dot'
					}]
				]
			};
		},
		onLoad() {
			this.getList(1);
		},
		onReachBottom() {
			if (this.list.length < this.count) {
				this.getList();
			}
		},
		methods: {
			handleCreateClassrule(cla_id) {
				this.$refs['CreateClassruleRef'].cla_id = cla_id;
				this.$refs['CreateClassruleRef'].teachingTypeOpen();
			},
			handleRouter(item, row) {
				this.$Router.push({
					path: item.path,
					query: {
						cla_id: row.id
					}
				});
			},
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				getClass(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							this.$lodash.forEach(list, (e) => {
								e.percentage = this.getPercentage(e);
							});
							this.list = this.list.concat(list);
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			},
			getPercentage(row) {
				const {
					cla_course_status_1,
					cla_course_status_all
				} = row;
				if (cla_course_status_1 || cla_course_status_all) {
					return Math.round((cla_course_status_1 / cla_course_status_all) * 100);
				} else return 0;
			},
			handleDetail(id) {
				this.$Router.push({
					path: '/class-detail',
					query: {
						id
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>